package cn.edu.neusoft.practice.Controller;
//cn.edu.neusoft.practice
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import cn.edu.neusoft.practice.mapper.UserMapper;
import cn.edu.neusoft.practice.mapper.SpecialMapper;
import cn.edu.neusoft.practice.po.Special;
import cn.edu.neusoft.practice.po.User;

@CrossOrigin
@Controller
public class UserController {

	//
	@Autowired
	private UserMapper UserMapper;
	
	@RequestMapping("/getAllUsers")
	@ResponseBody
	public List<User> getAllUsers(){
		return UserMapper.selectList(null); //针对单表的查询方法
	}
	
	@RequestMapping("/updateUser")//更新科室
	   @ResponseBody
	    public int updateUser(User r){
	      return UserMapper.updateById(r);

	    }
	    @RequestMapping("/addUser")//加
	    @ResponseBody
	    public int addUser(User u){
	      return UserMapper.insert(u);
	    }
	    
	    @RequestMapping("/updateSpecial")//只改Special属性
	    @ResponseBody
	    public List<User> updateSpecial(int userId){;
	      return UserMapper.updateSpecial(userId);
	    }
	    
	    @RequestMapping("/jiaSpecial")//加Special属性
	    @ResponseBody
	    public List<Special> jiaSpecial(User u){
	      return UserMapper.jiaSpecial(u);//Special里加对应值
	    }

	    @RequestMapping("/deleteUser")//删
	    @ResponseBody
	    public int deleUser(int userId){
	      return UserMapper.deleteById(userId);
	    }
	    
	    @RequestMapping("/updatePassword")//改密码
	    @ResponseBody
	    public int updatePassword(User e){
	      return UserMapper.updatepassword(e);
	    }
	    
	    @RequestMapping("/checkLogin")//登录逻辑
	    @ResponseBody
	    public User checkLogin(User user){
	      User u=UserMapper.checkLogin(user);
	      if(u!=null){
	        u.setLoginCount(u.getLoginCount()+1);
	        u.setLastLogin(getNow());
	        UserMapper.updateById(u);
	        return u;
	      }      
	      else
	    	System.out.print("失败");
	        return null;
	    }

	    public Date getNow()
	    {
	      SimpleDateFormat formatter= new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
	      Date date = new Date(System.currentTimeMillis());
	      return date;
	    }
	    
	    @RequestMapping("/registerUser")//注册
	    @ResponseBody
	    public int registerUser(User user){
	      User u=UserMapper.checkUserName(user);
	      if(u!=null)
	        return -1; //该用户名或手机号已经被占用
	      else      
	        return UserMapper.insert(user);      
	    }
}